package com.ruoyi.eam.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.Dept;
import com.ruoyi.eam.doman.User;
import com.ruoyi.eam.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.mapper.*;
import com.ruoyi.eam.doman.EamAsset;

/**
 * 固定资产Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-17
 */
@Service
public class EamAssetServiceImpl implements IEamAssetService
{
    @Autowired
    private EamAssetMapper eamAssetMapper;
    @Autowired
    private IDictDataService dictDataService;
    @Autowired
    private IEamTypeService eamTypeService;
    @Autowired
    private IEamLocationService eamLocationService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDeptService deptService;


    /**
     * 查询固定资产
     *
     * @param assetId 固定资产主键
     * @return 固定资产
     */
    @Override
    public EamAsset selectEamAssetByAssetId(Long assetId)
    {
        return eamAssetMapper.selectEamAssetByAssetId(assetId);
    }

    /**
     * 查询固定资产列表
     *
     * @param eamAsset 固定资产
     * @return 固定资产
     */
    @Override
    public List<EamAsset> selectEamAssetList(EamAsset asset)
    {
        List<EamAsset> list = eamAssetMapper.selectEamAssetList(asset);
        for (EamAsset eamAsset : list) {
            String useStatus = dictDataService.selectDictDataByDictCode(eamAsset.getUseStatus()).getDictLabel();
            String typeName = eamTypeService.selectEamTypeByTypeId(eamAsset.getTypeId()).getTypeName();
            if(eamAsset.getLocationId() != null){
                eamAsset.setLocationName(eamLocationService.selectEamLocationByLocationId(eamAsset.getLocationId()).getLocationName());
            }
            eamAsset.setTypeName(typeName);
            eamAsset.setStatusName(useStatus);
            if(eamAsset.getUsePerson() != null){
                //使用人
                User user = userService.selectUserByUserId(eamAsset.getUsePerson());
                eamAsset.setUseName(user.getNickName());
                //部门
                Dept dept = deptService.selectDeptByDeptId(user.getDeptId());
                eamAsset.setDeptName(dept.getDeptName());
                //领用时间

            }
        }
        return list;
    }

    /**
     * 新增固定资产
     *
     * @param eamAsset 固定资产
     * @return 结果
     */
    @Override
    public int insertEamAsset(EamAsset eamAsset)
    {
        eamAsset.setCreateTime(DateUtils.getNowDate());
        //编码规则
        if(eamAsset.getAssetCode() == null){
            //获取最后一条数据的编码
            String assetCodeLast = eamAssetMapper.queryEamAssetList().get(eamAssetMapper.queryEamAssetList().size()-1).getAssetCode();
            //截取第2到5的字符，获取流水号，加一
            String serial =  assetCodeLast.substring(2,5);
            if(serial.equals("999")){
                serial = "001";
            }else{
                int num = Integer.parseInt(serial) + 1;
                serial = String.format("%03d",num);
            }
            //获取当前时间
            String nowDate = DateUtils.getDateYYYYMMDD();
            //编码
            String assetCode = EamConstants.EAM_CODE_PR + serial +nowDate;
            eamAsset.setAssetCode(assetCode);
            eamAsset.setIsdel(1L);
        }
        return eamAssetMapper.insertEamAsset(eamAsset);
    }

    /**
     * 修改固定资产
     *
     * @param eamAsset 固定资产
     * @return 结果
     */
    @Override
    public int updateEamAsset(EamAsset eamAsset)
    {
        return eamAssetMapper.updateEamAsset(eamAsset);
    }

    /**
     * 批量删除固定资产
     *
     * @param assetIds 需要删除的固定资产主键
     * @return 结果
     */
    @Override
    public int deleteEamAssetByAssetIds(Long[] assetIds)
    {
        return eamAssetMapper.deleteEamAssetByAssetIds(assetIds);
    }

    /**
     * 删除固定资产信息
     *
     * @param assetId 固定资产主键
     * @return 结果
     */
    @Override
    public int deleteEamAssetByAssetId(Long assetId)
    {
        return eamAssetMapper.deleteEamAssetByAssetId(assetId);
    }

    @Override
    public int editUseStatus(List<EamAsset> assets) {
        for (EamAsset asset : assets) {
            int num = eamAssetMapper.updateEamAsset(asset);
            if(num == 0){
                return 0;
            }
        }
        return 1;
    }

    @Override
    public List<EamAsset> queryEamDateList(EamAsset eamAsset)
    {
        List<EamAsset> list = eamAssetMapper.queryEamAssetList();
        System.out.println(list);
        List<EamAsset> list1 = new ArrayList<>();
        for (EamAsset asset : list) {
            if(asset != null){
                Integer num =  eamAssetMapper.selectEamAssetCount(asset.getAssetId());
                System.out.println("num"+num);
                if(num > 0){
                    asset.setNum(num);
                    list1.add(asset);
                }
            }
        }
        return list1;
    }
}
